package com.douyu.ocean.serversCheck;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Map;
import java.util.ResourceBundle;

public class CheckMySqlRecord extends ServerCheck {

	private String url;
	private String user;
	private String passwd;
	private String format;
	private String scale;
	private String sql;
	private static ResourceBundle bundle;
	private static String check_invalid_mysql = "";
	private static String MYSQL_DRIVER = "com.mysql.jdbc.Driver";
	
	static{
		bundle = ResourceBundle.getBundle("monitor");
		if(bundle == null){
			log.error("can not find the monitor.properties");
		}
		check_invalid_mysql = bundle.getString("check_invalid_mysql");
	}
	
	public CheckMySqlRecord(Map<String, String> conf){
		this.url = conf.get("url");
		this.user = conf.get("user");
		this.passwd = conf.get("passwd");
		this.scale = conf.get("scale");
		this.format = conf.get("format");
		this.sql = conf.get("sql");
	}
	
	@Override
	public String checkHealth() {
		log.info("checking with MySQL record");
		String warning_message = "";
		
		Connection conn = null;
		try {
			Class.forName(MYSQL_DRIVER);
			conn = DriverManager.getConnection(this.url,this.user,this.passwd);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	
		String mysqlName = url.substring(url.indexOf("//")+2,url.lastIndexOf(":"));
		
		String part = sql.split("from")[1];
		String tableName = part.substring(1, part.indexOf(" ",1));
		try {
			Statement state = conn.createStatement();
		
			Calendar cal = null; 
			
			if(scale.equalsIgnoreCase("day")){
				cal = Calendar.getInstance();
				cal.add(Calendar.DATE, -1);
				String yesterday = new SimpleDateFormat(format).format(cal.getTime());
				log.info("checking with record of date : "+yesterday);
				
				String formatSql = String.format(sql, yesterday);
				ResultSet rs = state.executeQuery(formatSql);
				if(rs.next()){
					int num = rs.getInt(1);
					log.info("mysql record :"+num+", date : "+yesterday);
					if(num < 60000000 && tableName.equals("rpt_visitor_retention_day")){
						warning_message = warning_message + "MySQL loss record at :"+mysqlName+",table : rpt_visitor_retention_day";
						log.warn("MySQL loss record at :"+mysqlName+",table : rpt_visitor_retention_day");
					}
				}else{
					warning_message = warning_message + "MySQL no record at :"+mysqlName+",table :"+tableName;
					log.warn("MySQL no record at :"+mysqlName);
				}
				log.info("checked mysql record with scale of day");
					
			}else if(scale.equalsIgnoreCase("minute")){
				cal = Calendar.getInstance();
				int invalid = Integer.parseInt(check_invalid_mysql);
				cal.add(Calendar.MINUTE, -invalid);
				int sends = (int)(cal.getTimeInMillis()/1000)%600;
				cal.add(Calendar.SECOND, -sends);
				String time = new SimpleDateFormat(format).format(cal.getTime());
				cal.add(Calendar.MINUTE, -10);
				String lastTime = new SimpleDateFormat(format).format(cal.getTime());
				String formatSql = String.format(sql, time);
				
				String lastSql = String.format(sql,lastTime);
				log.info("last 10min sql :" + lastSql);
				log.info("this 10min sql :" + formatSql);
				ResultSet rs_old = state.executeQuery(lastSql);
				int oldNum = 0;
				while(rs_old.next()){
					oldNum = rs_old.getInt(1);
				}
				if(oldNum == 0){
					log.warn("select last record but no result");
				}
					
				ResultSet rs = state.executeQuery(formatSql);
				if(rs.first()){
					while(rs.next()){
						int num = rs.getInt(1);
						if(oldNum != 0 && num < (int)0.5*oldNum){
							warning_message = warning_message + "MySQL loss record at :" + mysqlName+",table :"+tableName;
							log.warn("MySQL loss record at :" + mysqlName+",table :"+tableName);
						}
					}
				}else{
					warning_message = warning_message + "MySQL no record at :"+mysqlName+",table :"+tableName;
					log.warn("MySQL no record at :"+mysqlName+",table :"+tableName);
				}
				log.info("checked mysql record with scale of minute");
			}else
				log.error("with wrong scale type while checking mysql record");
			
			state.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try{
				if(conn != null){
					conn.close();
				}
			} catch (Exception e){
				e.printStackTrace();
			}
		}
		return warning_message;
	}

}
